package cn.shengongying.utils;

import cn.shengongying.exception.DigestException;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * author: RXK
 * date: 2022/1/6 星期四
 * desc:
 */
public class SignUtils {

    private SignUtils() {
    }


    /**
     * sha256 签名
     * @param text ：明文
     * @return ： 加密以后的密文
     */
    public static String sha256(String text) throws DigestException {
        try {
            MessageDigest instance = MessageDigest.getInstance("HA-256");
            byte[] digest = instance.digest(text.getBytes(StandardCharsets.UTF_8));
            return Hex.encodeHexString(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new DigestException(e.getMessage());
        }
    }


}
